<?php
/**
 * Created by PhpStorm.
 * User: this
 * Date: 2018/12/22
 * Time: 9:37
 */

namespace app\admin\controller;
use app\admin\model\Set;

class Link extends Base{
    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        $model = new Set();
        $link_info = $model->getOne(array('setting_cod'=>'link'),'setting_value');
        $link = unserialize($link_info['setting_value']);
        $this->assign('link',$link);
        $this->assign('site_title','友情链接-管理后台-百步园艺');
        return $this->fetch('index');
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(){
        if(request()->isPost()){
            $data = input('post.');
            if(!isset($data['link_name'])){
                ajaxReturn(0,'链接名称不能为空!',0);
            }
            if(!isset($data['link_url'])){
                ajaxReturn(0,'链接地址不能为空!',0);
            }
            $model = new Set();
            $link_info = $model->getOne(array('setting_cod'=>'link'),'setting_value');
            $update = array();
            if($link_info['setting_value']){
                $arr = unserialize($link_info['setting_value']);
                $arr[] = $data;
                $arr = array_values($arr);
                $update['setting_value'] = serialize($arr);
            }else{
                $update['setting_value'] = serialize(array($data));
            }
            $result = $model->edit(array('setting_cod'=>'link'),$update);
            if($result){
                ajaxReturn('/baibu/admin/Link','添加成功!',1);
            }else{
                ajaxReturn(0,'添加失败!',0);
            }
        }
        $this->assign('site_title','添加链接-管理后台-百步园艺');
        $this->assign('nav','添加链接');
        $this->assign('url','/baibu/admin/Link/add');
        return $this->fetch('add');
    }

    public function del(){
        if(!$_GET){
            ajaxReturn(0,'参数错误!',-1);
        }
        $model = new Set();
        $link_info = $model->getOne(array('setting_cod'=>'link'),'setting_value');
        $link = unserialize($link_info['setting_value']);
        if(gettype($_GET['link_id']) == 'array'){
            foreach ($_GET['link_id'] as $k => $v){
                if(in_array($v,array_keys($link))){
                    unset($link[$v]);
                    $update['setting_value'] = serialize(array_values($link));
                    $result = $model->edit(array('setting_cod'=>'link'),$update);
                }
            }
        }else{
            if(in_array($_GET['link_id'],array_keys($link))) {
                unset($link[$_GET['link_id']]);
                $update['setting_value'] = serialize(array_values($link));
                $result = $model->edit(array('setting_cod'=>'link'),$update);
            }
        }
        if($result){
            ajaxReturn($_GET['link_id'],'删除成功!',1);
        }else{
            ajaxReturn(0,'删除失败!',0);
        }
    }
}